package com.abu.eduorder.controller;


import com.abu.eduorder.pojo.Order;
import com.abu.eduorder.service.OrderService;
import com.abu.eduorder.utils.JwtUtils;
import com.abu.utils.Resp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 生成订单信息
     *
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("createOrder/{courseId}")
    public Resp GeneratorOrderDetails(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        Order orderDetails = this.orderService.queryOrderDetails(courseId, JwtUtils.getMemberIdByJwtToken(request));
        if (orderDetails == null) {
            return Resp.error();
        }
        return Resp.ok().data("orderNo", orderDetails.getOrderNo());
    }

    /**
     * 查询订单状态
     *
     * @param orderId
     * @return
     */
    @GetMapping("getOrderInfo/{orderId}")
    public Resp getOrderInfo(@PathVariable("orderId") String orderId) {
        Order orderStatus = this.orderService.queryOrderInfo(orderId);
        if (orderStatus == null) {
            return Resp.error();
        }
        return Resp.ok().data("item", orderStatus);
    }

    /**
     * 查询订单是否支付成功
     * @param memberid
     * @param id
     * @return
     */
    @GetMapping("isBuyCourse/{memberid}/{id}")
    public boolean isBuyCourse(@PathVariable("memberid") String memberid, @PathVariable("id") String id) {
        //订单状态是1表示支付成功
        int count = orderService.count(new QueryWrapper<Order>().eq("member_id", memberid).eq("course_id", id).eq("status", 1));
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

}

